package com.tianji.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class ZhangSanServlet
 */
public class GetTime extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public GetTime() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		//response.getWriter().append("Served at: ").append(request.getContextPath());
		response.setCharacterEncoding("UTF-8");//设置输出内容的编码格式
		response.setContentType("text/html");//设置输出的文件类型
		PrintWriter out = response.getWriter();
		out.println("<!DOCTYPE html>");
		out.println("<HTML>");
		out.println("<HEAD><meta charset='UTF-8'><title>时间界面</title></HEAD>");
		out.println("<BODY><script type=\"text/javascript\">\r\n" + 
				"		function getCurDate(){\r\n" + 
				"			var d = new Date();//获取当前时间 返回一个月中的某一天（1-31）\r\n" + 
				"			var week;\r\n" + 
				"			switch (d.getDay()){//getDay()取的是一个0-6之间的数\r\n" + 
				"			case 1: week=\"星期一\"; break;\r\n" + 
				"			case 2: week=\"星期二\"; break;\r\n" + 
				"			case 3: week=\"星期三\"; break;\r\n" + 
				"			case 4: week=\"星期四\"; break;\r\n" + 
				"			case 5: week=\"星期五\"; break;\r\n" + 
				"			case 6: week=\"星期六\"; break;\r\n" + 
				"			default: week=\"星期天\";\r\n" + 
				"			}\r\n" + 
				"			var years = d.getFullYear();//返回一个表示年份的 4 位数字\r\n" + 
				"			var month = add(d.getMonth()+1);//返回表示月份的数字 返回值是0（一月）到11（十二月）之间的一个整数\r\n" + 
				"			var days = add(d.getDate());//可返回月份的某一天\r\n" + 
				"			var hours = add(d.getHours());//可返回时间的小时字段\r\n" + 
				"			var minutes = add(d.getMinutes());//可返回时间的分钟字段\r\n" + 
				"			var seconds=add(d.getSeconds());//可返回时间的秒\r\n" + 
				"			var ndate = years+\"年\"+month+\"月\"+days+\"日 \"+hours+\":\"+minutes+\":\"+seconds+\" \"+week;\r\n" + 
				"			var divT=document.getElementById(\"logInfo\");\r\n" + 
				"			divT.innerHTML= ndate;//获取ndate的内容把他赋值给divT\r\n" + 
				"		}\r\n" + 
				"		function add(temp){\r\n" + 
				"			if(temp<10) \r\n" + 
				"			return \"0\"+temp;\r\n" + 
				"			else return temp;\r\n" + 
				"		}\r\n" + 
				"			setInterval(\"getCurDate()\",1000);//setInterval不停地调用\r\n" + 
				"		</script>\r\n" + 
				"		<p id=\"logInfo\"></p></BODY>");
				out.println("</HTML>");
				out.flush();
				out.close();// 关闭输出流

	
	
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
